package cn.edu.tju.elm.service;

import cn.edu.tju.elm.model.Comment;
import cn.edu.tju.elm.repository.CommentRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * ClassName: CommentService
 * Package: cn.edu.tju.elm.service
 * Description: 评论服务层
 *
 * @Author System Generated
 * @Create 2025/10/30
 * @Version 1.0
 */
@Service
@Transactional
public class CommentService {
    
    private final CommentRepository commentRepository;

    public CommentService(CommentRepository commentRepository) {
        this.commentRepository = commentRepository;
    }

    public Comment addComment(Comment comment) {
        return commentRepository.save(comment);
    }

    public Optional<Comment> getCommentById(Long id) {
        return commentRepository.findById(id);
    }

    public Optional<Comment> getCommentByOrderId(Long orderId) {
        return commentRepository.findByOrderIdAndDeletedFalse(orderId);
    }

    public List<Comment> getCommentsByCustomerId(Long customerId) {
        return commentRepository.findByCustomerIdAndDeletedFalse(customerId);
    }

    public List<Comment> getCommentsByBusinessId(Long businessId) {
        return commentRepository.findByBusinessIdAndDeletedFalse(businessId);
    }

    public List<Comment> getCommentsByRating(Integer rating) {
        return commentRepository.findByRatingAndDeletedFalse(rating);
    }

    public List<Comment> getCommentsByBusinessIdAndRating(Long businessId, Integer rating) {
        return commentRepository.findByBusinessIdAndRatingAndDeletedFalse(businessId, rating);
    }

    public Comment updateComment(Comment comment) {
        return commentRepository.save(comment);
    }

    public void deleteComment(Long id) {
        commentRepository.deleteById(id);
    }

    public boolean hasCommented(Long orderId) {
        return commentRepository.existsByOrderIdAndDeletedFalse(orderId);
    }

    public Double getAverageRatingByBusinessId(Long businessId) {
        Double avg = commentRepository.findAverageRatingByBusinessId(businessId);
        return avg != null ? avg : 0.0;
    }

    public Long getCommentCountByBusinessId(Long businessId) {
        return commentRepository.countByBusinessIdAndDeletedFalse(businessId);
    }

    public List<Comment> getAllComments() {
        return commentRepository.findByDeletedFalse();
    }

    /**
     * 获取店铺评论统计信息
     */
    public Map<String, Object> getBusinessCommentStatistics(Long businessId) {
        Map<String, Object> statistics = new HashMap<>();
        
        List<Comment> comments = commentRepository.findByBusinessIdAndDeletedFalse(businessId);
        statistics.put("totalComments", comments.size());
        
        if (!comments.isEmpty()) {
            // 平均评分
            Double averageRating = getAverageRatingByBusinessId(businessId);
            statistics.put("averageRating", averageRating);
            
            // 各星级评论数量
            Map<Integer, Long> ratingDistribution = new HashMap<>();
            for (int i = 1; i <= 5; i++) {
                int rating = i;
                long count = comments.stream().filter(c -> c.getRating() == rating).count();
                ratingDistribution.put(rating, count);
            }
            statistics.put("ratingDistribution", ratingDistribution);
            
            // 好评率（4-5星）
            long positiveCount = comments.stream().filter(c -> c.getRating() >= 4).count();
            double positiveRate = (double) positiveCount / comments.size() * 100;
            statistics.put("positiveRate", positiveRate);
            
        } else {
            statistics.put("averageRating", 0.0);
            statistics.put("ratingDistribution", new HashMap<>());
            statistics.put("positiveRate", 0.0);
        }
        
        return statistics;
    }
}



